package com.ourteam.frame.web;

import net.dao.BaseQueryBean;

import org.apache.commons.lang.ArrayUtils;

public abstract class AbstractStaticArraySearchHelper<T extends BaseQueryBean, S extends AbstractSearchModel<T>>
		extends AbstractSearchHelper<T, S> {

	private Object[] dataArray;

	public AbstractStaticArraySearchHelper(S searchModel) {
		super(searchModel);
	}

	abstract protected Object[] getData(S searchModel) throws Exception;

	@Override
	protected int queryCount(T queryBean) throws Exception {
		dataArray = getData(this.getSearchModel());
		return ArrayUtils.isNotEmpty(dataArray) ? dataArray.length : 0;
	}

	@Override
	protected Object[] queryData(T queryBean) throws Exception {
		if (queryBean.getMaxResults() == -1 || queryBean.getMaxResults() >= dataArray.length) {
			return dataArray;
		} else {
			return ArrayUtils.subarray(dataArray, queryBean.getStart(),
					queryBean.getStart() + queryBean.getMaxResults());
		}
	}

}
